<?php
namespace Home\Controller;
use  Think\Controller;

class MoneyController extends CommonController{

    public function  money(){

        $id = session(C(SESSIONUSER));
        $res_user = M('user')->where('id = "'.$id.'"')->find();

        $level = $res_user['level'];
        if($level == 1){
            $res_user['level'] = '普通会员';
        }else if($level == 2){
            $res_user['level'] = 'vip会员';
        }else if($level == 3){
            $res_user['level'] = '天使合伙人';
        }else{
            $res_user['level'] = '梦想合伙人';
        }
        $table1 = 'detail'.$level;
        $table2 = 'pic'.$level;

        $res_detail = M($table1)->where("id = '".$res_user['detailid']."'")->find();
        $res_pic = M($table2)->where("id = '".$res_user['picid']."'")->find();

        if($res_user['pid'] != 0){

            $res2 = M('user')->where('id ="'.$res_user['pid'].'"')->find();
            $res_user['parent'] = $res2['user'];
        }else{
            $res_user['parent'] = '无';
        }

        $res_user['phone'] = $res_detail['phone'];
        $res_user['addres'] = $res_detail['addres'];

        $res_num = M('cutnum')->group('userId')->field('sum(num) as number,userId')->order('number')->select();

        foreach($res_num as $k=>$v){

            if($id == $v['userid']){
                $res_user['num'] = $v['number'];
                $res_user['rank'] = $k+1;
            }
        }

        $where['num'] = array('LT','0');
        $where['userId'] = array('eq',$id);

        $res3 = M('cutnum')->where($where)->field('sum(num) as number')->select();
        $res4 = M('cutnum')->where('userId = "'.$id.'"')->field('sum(num) as number')->select();

        $res_user['cnum'] = abs($res3[0]['number'])+ abs($res4[0]['number']);
        $res_user['xnum'] = abs($res3[0]['number']);// 已用名额
        $res_user['snum'] = abs($res4[0]['number']);// 剩余名额

        $config = M('system1')->where('id = 1')->find();


        $this->assign('list',$res_user);
        $this->assign('config',$config);

        $this->display();
    }

    // 会员充值升级  升级为VIP

    public function firstupdate($money,$level,$res_user){

        $money = $money;
        $level = $level;  // 要升级的等级
        $res_user = $res_user;

        $dbtable = 'detail'.$res_user['level'];
        // 根据detailId 获取详情表里的邀请者ID
        $get_detail = M($dbtable)->where("id = '".$res_user['detailid']."'")->find();
        // 根据邀请者ID 获取邀请者信息
        $useredinfo = M('user')->where("id = '".$get_detail['useredid']."'")->find();

        if(!$useredinfo){

            $msg_log = APP_PATH . 'userinfo.log';
            monitor_log($msg_log, array('useredinfo'=>$useredinfo,'levle'=>$level), FILE_APPEND);

            $data = genBody('1','参数错误');
            $this->ajaxReturn($data,'JSON');
        }

        if($useredinfo && $useredinfo['level'] != 2){ // 如果邀请者为天使合伙人或者梦想合伙人  TODO:本方法目前适用于升级vip
            $create_money['userId'] = $useredinfo['id'];
        }else if($useredinfo && $useredinfo['level'] == 2){
              // 如果邀请者为VIP会员
            $create_money['userId'] = $useredinfo['pid'];  // userId 指的是获得金钱的id  如果金钱数目为正数，则收入 否则 为支出
        }

//        var_dump($create_money['userId']);die;

         //检测选择的上级是否可以进行权限购买操作
        $checkstatus = $this->checkuserstatus($create_money);

        if($checkstatus){

            $data = genBody('1','您选择的上级目前不能进行名额操作，请更换');
            $this->ajaxReturn($data,'JSON');
        }
        // 想要升级为天使，需要去后台发送申请，由后台确认是否升级成功
        //
        if($level == 3){
            // 向后台发送申请
            $arr_apply1['userId'] = $res_user['id'];
            $arr_apply1['pid'] = $create_money['userId'];
            $arr_apply1['level'] = $level;
            $arr_apply1['money'] = $money;
            $arr_apply1['time'] = time();

            $table = 'apply'.$level;
//            $result_apply = M($table)->add($arr_apply1);

            $res_user_pid = M('user')->where('id = "'.$create_money['userId'].'"')->find();
            if($level == $res_user_pid['level']){  // 如果天使下面的vip想要升级为天使，则想天使发送消息 您的下级要升级为天使，请问您是否升级。

                $arr_apply1['allow'] = 4; //暂时停止操作
                $arr_apply1['reson'] = 3; // 上级权限不够
                $result_apply = M($table)->add($arr_apply1);

                // 由于该上级的权限不足，因此发送系统公告
                $arr_notice4['noticeId'] = 4;
                $arr_notice4['userId'] = $create_money['userId'];
                $arr_notice4['time'] = time();
                $result_notice = M('notice')->add($arr_notice4);

                // 向天使合伙人发送申请公告
                $arr_notice['noticeId'] = 5;
                $arr_notice['time'] = time();
                $arr_notice['userId'] = $create_money['userId'];
                $arr_notice['objectId'] = $res_user['id'];
                $arr_notice['level'] = 3;
                $arr_notice['applyId'] = $result_apply;
                $table = 'notice3';
                $res_notice = M($table)->add($arr_notice);

            }else{

                $result_apply = M($table)->add($arr_apply1);

                $arr_notice['noticeId'] = 5;
                $arr_notice['time'] = time();
                $arr_notice['userId'] = $create_money['userId'];
                $arr_notice['objectId'] = $res_user['id'];
                $arr_notice['level'] = 3;
                $arr_notice['applyId'] = $result_apply;
                $table = 'notice3';
                $res_notice = M($table)->add($arr_notice);

            }
        }else if($level==2){
            // 升级到vip 无需向后台申请 可以直接由他的上级进行审核
            // 需要后台把控处理问题
            $arr_apply3['userId'] = $res_user['id'];
            $arr_apply3['pid'] = $create_money['userId'];
            $arr_apply3['level'] = $level;
            $arr_apply3['money'] = $money;
            $arr_apply3['time'] = time();

            $table = 'apply2';
            $result_apply = M($table)->add($arr_apply3);

            $arr_notice['noticeId'] = 6;
            $arr_notice['time'] = time();
            $arr_notice['userId'] = $create_money['userId'];
            $arr_notice['objectId'] = $res_user['id'];
            $arr_notice['level'] = 2;
            $arr_notice['applyId'] = $result_apply;
            $table = 'notice2';
            $res_notice = M($table)->add($arr_notice);

        }

        if($res_notice){

             return 1;
        }

    }

   // 升级权限  会员->vip->天使
    // level 要求升级的等级
    // 因为是通过邀请码登陆的，所有不需要指定目标
    public function addMoney(){

        $where['id'] = session(C(SESSIONUSER));
        $level = I('level');

        $result1 = $this->checkapply($where['id']);
        if($result1){
            $data = genBody('2','您还有申请未结束，请结束后再进行操作');
            $this->ajaxReturn($data,'JSON');
        }

        $num_user = M('user')->count(); // 如果系统内没有其他会员 则必须升级为梦想合伙人
        if($num_user == 2){

            if($level != 4){
                $data = genBody('3','当前没有梦想会员，请升级为梦想会员');
                $this->ajaxReturn($data,'JSON');
            }
        }

        // 如果用户为升级未成功人员 则需要先选择合适的上级，成为正常用户后，再来升级
        $res_status = M('status')->where("userId = '".$where['id']."' and status = 1")->find();

        if($res_status){
            $data = genBody('4','您正处于特殊状态,需要先成为正式会员才能继续申请');
            $this->ajaxReturn($data,'JSON');
        }


        $res_money = M('system1')->where('id = 1')->find();

        if($level == 2){
           $money = $res_money['vip'];

           $status3 = M('apply2')->where('userId = "'.$where['id'].'"')->find();
            if($status3){
                if($status3['allow'] != 2 && $status3['allow'] != 3){
                    $data = genBody('5','您还有申请未结束，请结束后再进行操作');
                    $this->ajaxReturn($data,'JSON');
                }
            }

        }else if($level == 3){

            $money = $res_money['first'];

            $status3 = M('apply3')->where('userId = "'.$where['id'].'"')->find();
            if($status3){
                if($status3['allow'] != 2 && $status3['allow'] != 3){
                    $data = genBody('6','您还有申请未结束，请结束后再进行操作');
                    $this->ajaxReturn($data,'JSON');
                }
            }
        }else if($level == 4){

            $status3 = M('apply4')->where('userId = "'.$where['id'].'"')->find();
            if($status3){
                if($status3['allow'] != 2 && $status3['allow'] != 3){
                    $data = genBody('7','您还有申请未结束，请结束后再进行操作');
                    $this->ajaxReturn($data,'JSON');
                }
            }


            $result_max = $this->maxmoney($res_money['second']);
            if($result_max){

                $data = genBody('1','您的申请已经发送成功，客服人员会就此询问您是否付款成功，请耐心等待');
            $this->ajaxReturn($data,'JSON');

            }
        }

        $res_user = M('user')->where($where)->find();

        if($res_user['level'] > $level){

            $data = genBody('8','您不能升级比您当前低的等级');
            $this->ajaxReturn($data,'JSON');
        }

        if($res_user['level'] == $level){

            if($level == 2){

                $data = genBody('9','VIP不需要重新购买');
                $this->ajaxReturn($data,'JSON');

            }else if($level == 3){

                $status3 = M('apply3')->where('userId = "'.$where['id'].'"')->find();
                if($status3){
                    if($status3['allow'] == 2 || $status3['allow'] == 3){
                        $data = genBody('10','您还有申请未结束，请结束后再进行操作');
                        $this->ajaxReturn($data,'JSON');
                    }
                }

                $res_repeart = $this->repeartmoney($money);

                if($res_repeart){
                    $data = genBody('1','您的申请已经发送成功，客服人员会就此询问您是否付款成功，请耐心等待');
                    $this->ajaxReturn($data,'JSON');
                }
            }
        }

        $result_user_apply = $this->firstupdate($money,$level,$res_user);

        if($result_user_apply == 1){

            $data = genBody('1','您的申请已经发送成功，客服人员会就此询问您是否付款成功，请耐心等待');
            $this->ajaxReturn($data,'JSON');
        }

    }

    public function checkapply($uid){

        $where['userId'] = array('eq',$uid);
        $where['allow'] = array('not in','2,3');
//        $where1['allow'] = array('neq',3);


        $res1 =M('apply2')->where($where)->find();
        $res2 =M('apply3')->where($where)->find();
        $res3 =M('apply4')->where($where)->find();

        if($res1 || $res2 || $res3){

            return 1;
        }

    }

    // 成为梦想合伙人
    public function maxmoney($money){

        // TODO  ::测试 假定已经获得平台的权限

        $userId = session(C(SESSIONUSER));

        $res_status = M('status')->where("userId = '".$userId."' and state =1")->find();

        if($res_status){

            $data = genBody('1','您正处于特殊状态，请先接受邀请成为正式用户');
            $this->ajaxReturn($data,'JSON');
        }

        // 发出申请
        $table = 'apply4';
        $arr_apply['userId'] = $userId;
        $arr_apply['pid'] = 0;
        $arr_apply['level'] = 4;
        $arr_apply['money'] = $money;
        $arr_apply['time'] = time();

        $res_apply = M($table)->add($arr_apply);// 向后台发出申请

        if($res_apply){
            return 1;
        }
    }


    // 重新购买权限

    public function repeartmoney($money){

        $id = session(C(SESSIONUSER));
        $res_user = M('user')->where('id = "'.$id.'"')->find();

        $level = $res_user['level'];

        // 发送重复购买的申请
        $arr_repeart['level'] = $level;
        $arr_repeart['userId'] = $id;
        $arr_repeart['pid'] = $res_user['pid'];
        $arr_repeart['money'] = $money;
        $arr_repeart['time'] = time();

        $table = 'reapply'.$level;
        $res_repeart = M($table)->add($arr_repeart);

        // 向他的上级发出系统公告
        $arr_notice['noticeId'] = 5;
        $arr_notice['time'] = time();
        $arr_notice['userId'] = $res_user['pid'];
        $arr_notice['objectId'] = $res_user['id'];
        $arr_notice['level'] = 3;
        $arr_notice['applyId'] = $res_repeart;
        $table = 'notice3';
        $res_notice = M($table)->add($arr_notice);

        if($res_repeart){
            return 1;
        }
    }


    public function  checkuserstatus($create_money){

        // 判断选择的上级是否可以权限购买操作
        $get_res_level = M('user')->where("id = '".$create_money['userId']."'")->find();

        $table = 'userstatus'.$get_res_level['level'];

        $res_userstatus = M($table)->where("userId = '".$create_money['userId']."' and status = 2")->find();

        if($res_userstatus){

            return 1;
        }

    }

}
